// pages/my/my.js
const app = getApp();
const util = require('../../utils/time.js')
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuBorder: false,
    menuArrow: true,
    menuCard: true,
    userinfo: null,
    faceImg: null
    // ../../images/user1.jpeg
  },
  toaboutOur() {
    wx.navigateTo({
      url: '../aboutOur/aboutOur',
    })
  },
  toMyActivity() {
    console.log('我的志愿活动')
    wx.navigateTo({
      url: '../myActivity/index',
    })
  },
  toMyMess() {
    wx.navigateTo({
      url: '../myMess/index',
    })
  },
  navigtorToPerson() {
    wx.navigateTo({
      url: '../person-information/index',
    })
  },
  toLogin() {
    wx.navigateTo({
      url: '../login/index',
    })
  },
  ChooseImage() {
    var that = this
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], //从相册选择
      success: (res) => {
        wx.showLoading({
          title: '图片上传中...',
        })
        var time = util.formatTime(new Date()) // 获取时间
        var FilePaths = []
        const temFilePaths = res.tempFilePaths
        // console.log('这是res.tempFilePaths', res.tempFilePaths.length)
        let promiseArr = []
        let promise = new Promise((resolve, reject) => {
          var randstring = Math.floor(Math.random() * 1000000).toString() + '.png'
          randstring = time + '-' + randstring
          wx.cloud.uploadFile({
            cloudPath: 'orderimg/' + randstring,
            filePath: temFilePaths[0],
            success: res => {
              // get resource ID
              console.log(res.fileID)
              FilePaths[0] = res.fileID
              resolve(res)
            },
            fail: err => {
              reject(error);
            }
          })
        })
        promiseArr.push(promise)
        Promise.all(promiseArr).then((result) => {
          console.log('上传图片1', FilePaths[0])
          that.setData({
            faceImg: FilePaths[0]
          })
          app.globalData.userinfo.faceImg = FilePaths
          db.collection('user').where({
            username: app.globalData.userinfo.username
          }).update({
            data: {
              faceImg: FilePaths[0]
            }
          })
          wx.hideLoading()
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('userinfo', app.globalData.userinfo)
    this.setData({
      userinfo: app.globalData.userinfo,
      faceImg: app.globalData.userinfo.faceImg
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  // 跳转到设置页面
  toSet() {
    wx.navigateTo({
      url: '../set/index',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userinfo: app.globalData.userinfo,
      faceImg: app.globalData.userinfo.faceImg
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})